package com.javabi.htmlbuilder.html;

import com.javabi.htmlbuilder.html.element.Body;
import com.javabi.htmlbuilder.html.element.Head;

/**
 * An HTML Document Element.
 * <p>
 * &lt;html&gt; ... &lt;/html&gt;
 * </p>
 * @see Head
 * @see Body
 */
public class HTMLDocument extends HTMLElement<HTMLDocument> {

	/** The head element. */
	private Head head = null;
	/** The body element. */
	private Body body = null;

	/**
	 * Creates a new document.
	 */
	public HTMLDocument() {
	}

	/**
	 * Creates a new document.
	 * @param head the head.
	 * @param body the body.
	 */
	public HTMLDocument(Head head, Body body) {
		this.head = head;
		this.body = body;
	}

	/**
	 * Returns the head, creating a new one if necessary.
	 * @return the head
	 */
	public Head head() {
		if (head == null) {
			head = new Head();
		}
		return head;
	}

	/**
	 * Returns the head
	 * @return the head
	 */
	public Head getHead() {
		return head;
	}

	/**
	 * Sets the head
	 * @param head the head to set
	 */
	public void setHead(Head head) {
		this.head = head;
	}

	/**
	 * Returns the body, creating a new one if necessary.
	 * @return the body
	 */
	public Body body() {
		if (body == null) {
			body = new Body();
		}
		return body;
	}

	/**
	 * Returns the body
	 * @return the body
	 */
	public Body getBody() {
		return body;
	}

	/**
	 * Sets the body
	 * @param body the body to set
	 */
	public void setBody(Body body) {
		this.body = body;
	}

	/**
	 * Returns true to format this element.
	 * @return true to format this element.
	 */
	public boolean format() {
		return true;
	}

	/**
	 * Returns the element name.
	 * @return the element name.
	 */
	public HTMLElementName getElementName() {
		return HTMLElementName.HTML;
	}

	/**
	 * Append this to the given html builder.
	 * @param html the html builder.
	 * @return the html builder.
	 */
	public HTMLBuilder appendTo(HTMLBuilder html) {
		html.open(this, false);
		appendInlineStyleAttributesTo(html);
		if (head != null) {
			head.appendTo(html);
		}
		if (body != null) {
			body.appendTo(html);
		}
		html.close(this);
		return html;
	}

}
